﻿using BooksOnline.Models.Entities;
using FluentNHibernate.Cfg;
using FluentNHibernate.Cfg.Db;
using NHibernate;

namespace BooksOnline.Repositories.Base
{
    public class NHibernateSessionProvider
    {
        private static ISessionFactory sessionFactory;

        private static ISession session;

        public static ISession CurrentSession
        {
            get { return session; }
            set { session = value; }
        }

        public static void SetSessionFactory(ISessionFactory _sessionFactory)
        {
            sessionFactory = _sessionFactory;
        }

        public static void SetSessionFactory()
        {
            // Set default to SQL 2005 database
            if(sessionFactory == null)
            {
                sessionFactory =
                    Fluently
                        .Configure()
                        .Database(MsSqlConfiguration
                                      .MsSql2005
                                      .ConnectionString(
                                          "Password=Welcome@123;Persist Security Info=True;" +
                                          "User ID=sa;Initial Catalog=BooksOnline;Data Source=UNIVERSE"))
                        .Mappings(a => a.FluentMappings.AddFromAssemblyOf<Book>())
                        .BuildSessionFactory();
            }
        }
    }
}